package com.cnap.controller;

import com.cnap.CnapApplicationTests;
import com.cnap.model.common.PageResult;
import com.cnap.model.log.OperationTypeEnum;
import com.cnap.model.progress.ProgressVOForSplit;
import com.cnap.model.schedule.DistributeParam;
import com.cnap.model.schedule.OperateParam;
import com.cnap.model.schedule.OperateTypeEnum;
import com.cnap.model.schedule.QueryMicroServiceParam;
import com.cnap.model.schedule.ScaleStrategyDTO;
import com.cnap.model.schedule.StrategyDTO;
import com.cnap.model.schedule.ValidateParam;
import com.cnap.service.MicroSvcService;
import com.cnap.service.OperateService;
import com.cnap.service.OperationLogService;
import com.cnap.service.ProgressService;
import com.cnap.service.ScheduleService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;

import java.util.Collections;
import java.util.List;

/**
 * <类文件描述>
 *
 * @author xc
 **/
class MicroServiceControllerTest extends CnapApplicationTests {

    @Autowired
    private MicroServiceController microServiceController;
    @MockBean
    private MicroSvcService microSvcService;
    @MockBean
    private ProgressService progressService;
    @MockBean
    private OperationLogService operationLogService;
    @MockBean
    private ScheduleService scheduleService;
    @MockBean
    private OperateService operateService;

    @Test
    void listMicroService() {
        QueryMicroServiceParam param = new QueryMicroServiceParam();
        Mockito.when(microSvcService.findByPage(param)).thenReturn(new PageResult<>());

        Assertions.assertDoesNotThrow(()->{
            microServiceController.listMicroService(param);
        });
    }

    @Test
    void splitMicroServices() {
        Mockito.doNothing().when(microSvcService).splitMicroServices("appId");
        Mockito.when(operationLogService.saveOperationLog(OperationTypeEnum.OTHER.getType(), "admin分解了应用")).thenReturn(true);

        Assertions.assertDoesNotThrow(()->{
            microServiceController.splitMicroServices("appId");
        });
    }

    @Test
    void queryProgress() {
        Mockito.when(progressService.getProgressForSplitByApplicationId("appId")).thenReturn(new ProgressVOForSplit());
        Assertions.assertDoesNotThrow(()->{
            microServiceController.queryProgress("appId");
        });
    }

    @Test
    void addScheduleStrategyToMicroServices() {
        List<StrategyDTO> strategyDTOList = Collections.emptyList();

        Mockito.doNothing().when(scheduleService).addScheduleStrategy(strategyDTOList);
        Mockito.when(operationLogService.saveOperationLog(OperationTypeEnum.ADD.getType(), "admin添加了调度策略")).thenReturn(true);

        Assertions.assertDoesNotThrow(()->{
            microServiceController.addScheduleStrategyToMicroServices(strategyDTOList);
        });
    }

    @Test
    void addScaleStrategyToMicroServices() {
        List<StrategyDTO> strategyDTOList = Collections.emptyList();

        Mockito.doNothing().when(scheduleService).addScheduleStrategy(strategyDTOList);
        Mockito.when(operationLogService.saveOperationLog(OperationTypeEnum.ADD.getType(), "admin添加了调度策略")).thenReturn(true);

        List<ScaleStrategyDTO> strategyDTOS = Collections.emptyList();
        Assertions.assertDoesNotThrow(()->{
            microServiceController.addScaleStrategyToMicroServices(strategyDTOS);
        });
    }

    @Test
    void deleteStrategyById() {
        Mockito.when(scheduleService.deleteStrategyById("strId")).thenReturn(true);
        Mockito.when(operationLogService.saveOperationLog(OperationTypeEnum.ADD.getType(), "admin删除了策略")).thenReturn(true);

        Assertions.assertDoesNotThrow(()->{
            microServiceController.deleteStrategyByIds(Collections.singletonList("strId"));
        });
    }

    @Test
    void getYamlByServiceId() {
        Mockito.when(microSvcService.getYamlByServiceId("svcId")).thenReturn("yaml");

        Assertions.assertDoesNotThrow(()->{
            microServiceController.getYamlByServiceId("svcId");
        });
    }

    @Test
    void validate() {
        ValidateParam param = new ValidateParam();
        Mockito.when(microSvcService.validate(param)).thenReturn(true);

        Assertions.assertDoesNotThrow(()->{
            microServiceController.validate(param);
        });
    }

    @Test
    void getMicroServicesByApplicationId() {
        Mockito.when(microSvcService.selectMicroServicesByAppId("appId")).thenReturn(Collections.emptyList());

        Assertions.assertDoesNotThrow(()->{
            microServiceController.getMicroServicesByApplicationId("appId");
        });
    }

    @Test
    void distribute() {
        DistributeParam param = new DistributeParam();
        Mockito.when(microSvcService.distribute(param)).thenReturn(true);
        Mockito.when(operationLogService.saveOperationLog(OperationTypeEnum.ADD.getType(), "admin部署了微服务")).thenReturn(true
        );

        Assertions.assertDoesNotThrow(()->{
            microServiceController.distribute(param);
        });
    }

    @Test
    void operationWithDelete() {
        OperateParam param = new OperateParam();
        param.setOperationType(OperateTypeEnum.DELETE.getType());
        Mockito.when(operateService.operate(param)).thenReturn(true);
        Mockito.when(operationLogService.saveOperationLog(OperationTypeEnum.ADD.getType(), "admin删除了微服务")).thenReturn(true);

        Assertions.assertDoesNotThrow(()->{
            microServiceController.operation(param);
        });
    }
    @Test
    void operationWithOther() {
        OperateParam param = new OperateParam();
        param.setOperationType(OperateTypeEnum.SUSPEND.getType());
        Mockito.when(operateService.operate(param)).thenReturn(true);
        Mockito.when(operationLogService.saveOperationLog(OperationTypeEnum.ADD.getType(), "admin暂停了微服务")).thenReturn(true);

        Assertions.assertDoesNotThrow(()->{
            microServiceController.operation(param);
        });
    }
}